//
//  CombinedPunchTableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2025/3/11.
//

#import "CombinedPunchTableViewCell.h"


@interface CombinedPunchTableViewCell ()
@property (nonatomic ,strong) UILabel * executeTimeLab;
@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,strong) UIButton * ssBtn;
///s
@property (nonatomic ,strong) UIButton * SeledBtn;
@end
@implementation CombinedPunchTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIView * backview = [UIView new];
    backview.addYJ(kwt(13));
    backview.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(100, kwt(50)) direction:GradientColorDirectionVertical startColor:KcolorHex(@"#F8FCFF") endColor:KcolorHex(@"#EAF2F9")];
    [self.contentView addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(5));
        make.bottom.offset(kwt(-5));
    }];

    
    
    UILabel * executeTimeLab = [DTKTool labelWithText:@"-" andFont:kCFont(15) andTextColor:KcolorHex(@"#222222")];
    self.executeTimeLab = executeTimeLab;
    [backview addSubview:executeTimeLab];
    [executeTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(36));
            make.top.offset(kwt(14));
            make.height.offset(kwt(15));
    }];
    
    
    UIButton * ssbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ssbtn setImage:[UIImage imageNamed:@"expand"] forState:UIControlStateNormal];
    [ssbtn setImage:[UIImage imageNamed:@"fold"] forState:UIControlStateSelected];
    self.ssBtn = ssbtn;
    [ssbtn addTarget:self action:@selector(ssBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:ssbtn];
    [ssbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.centerY.equalTo(executeTimeLab);
        make.height.width.offset(kwt(16));
    }];
    
    
    UIButton * SeledBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [SeledBtn setImage:[UIImage imageNamed:@"not_selected"] forState:UIControlStateNormal];
    [SeledBtn setImage:[UIImage imageNamed:@"select_all"] forState:UIControlStateSelected];
    self.SeledBtn = SeledBtn;
    [SeledBtn addTarget:self action:@selector(seledBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:SeledBtn];
    [SeledBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.centerY.equalTo(executeTimeLab);
        make.height.width.offset(kwt(22));
    }];
    
    
    UIView * makeView = [UIView new];
    self.makeView = makeView;
    makeView.clipsToBounds = YES;
    makeView.backgroundColor = UIColor.whiteColor;
    makeView.addYJ(kwt(13));
    [backview addSubview:makeView];
    [makeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(kwt(43));
    }];
    
}
- (void)ssBtnClick {
    self.model.isSS = !self.ssBtn.isSelected;
    if(self.updateBlock){
        self.updateBlock();
    }
}
- (void)seledBtnClick {
    if(self.model.isSeled){///是选中状态
        for (CombinedPunchModel * m in self.model.modelArray) {
            m.isSeled = NO;
        }
        if(self.updateBlock){
            self.updateBlock();
        }
    } else {
        if(self.seledDayBlock){
            self.seledDayBlock(self.model);
        }
    }
}

-(void)setModel:(CombinedPunchDayModel *)model{
    _model = model;
    self.executeTimeLab.text = [NSDate br_stringFromDate:[NSDate br_dateFromString:model.executeTime dateFormat:@"yyyy-MM-dd"] dateFormat:@"yyyy年MM月dd日"]  ;
    self.ssBtn.selected = model.isSS;
    self.SeledBtn.selected = model.isSeled;
    [self.makeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat Y = 0;
    MJWeakSelf
    for (CombinedPunchModel * m in model.modelArray) {
        CombinedPunchSubclassView * view = [CombinedPunchSubclassView newViewWithModel:m];
        view.seledModelBlock = ^(CombinedPunchModel * _Nonnull Model, BOOL isSeled) {
            if(isSeled){///选中
                if(weakSelf.seledModelBlock){
                    weakSelf.seledModelBlock(Model);
                }
            } else {
                Model.isSeled = isSeled;
                if(weakSelf.updateBlock){
                    weakSelf.updateBlock();
                }
            }
        };
        view.openBlock = ^(CombinedPunchModel * _Nonnull model) {
            if(self.updateBlock){
                self.updateBlock();
            }
        };
        [self.makeView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.top.offset(Y);
            make.height.offset(m.viewH);
        }];
        
        UIView * line = [UIView new];
        line.backgroundColor = KcolorHex(@"#DBDBDB");
        [self.makeView  addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_bottom);
            make.left.offset(kwt(12));
            make.right.offset(kwt(-12));
            make.height.offset(1);
        }];
        
        Y += m.viewH;
    }
    
    
}

@end

@interface CombinedPunchSubclassView ()
@property (nonatomic ,strong) UIButton * openBtn;
@end

@implementation CombinedPunchSubclassView : UIView
+ (instancetype)newViewWithModel:(CombinedPunchModel *) model{
    CombinedPunchSubclassView * view = [CombinedPunchSubclassView new];
    view.model = model;
    [view setupView];
    return  view;
}
- (void)setupView{
    self.backgroundColor = UIColor.whiteColor;
    UIImageView * img = kimageView(@"DKtime");
    [self addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(14));
        make.left.offset(kwt(12));
        make.width.height.offset(kwt(18));
    }];
    
    UILabel * lab = [DTKTool labelWithText:[NSString stringWithFormat:@"%@ %@",self.model.executeTimePart,self.model.name] andFont:kCFont(17) andTextColor:KcolorHex(@"#0967FF")];
    [self addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(img);
        make.left.offset(kwt(34));
    }];
    
    UIButton * SeledBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [SeledBtn setImage:[UIImage imageNamed:@"not_selected"] forState:UIControlStateNormal];
    [SeledBtn setImage:[UIImage imageNamed:@"select_all"] forState:UIControlStateSelected];
    [SeledBtn addTarget:self action:@selector(seledBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:SeledBtn];
    SeledBtn.selected = self.model.isSeled;
    [SeledBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.centerY.equalTo(lab);
        make.height.width.offset(kwt(22));
    }];
    
    UIButton * openBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [openBtn setImage:[UIImage imageNamed:@"expand_pond2"] forState:UIControlStateNormal];
    [openBtn setImage:[UIImage imageNamed:@"expand_pond"] forState:UIControlStateSelected];
    [openBtn addTarget:self action:@selector(openBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:openBtn];
    openBtn.selected = self.model.isOpen;
    openBtn.hidden = self.model.isHidden;
    [openBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(43));
        make.left.offset(kwt(12));
        make.height.width.offset(kwt(17));
    }];
    
    UILabel * lab2 = [DTKTool labelWithText:self.model.pondNameStr andFont:kFont(17) andTextColor:KcolorHex(@"#626262")];
//    lab2.numberOfLines = 0;
    [self addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(43));
        make.left.offset(kwt(34));
        make.width.offset(kwt(280));
        make.height.offset(kwt(17));
    }];
    
    if(self.model.isOpen){
        UIView * backview = [UIView new];
        backview.backgroundColor = KcolorHex(@"#F5F6F6");
        backview.addYJ(kwt(8.5));
        [self addSubview:backview];
        [backview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kwt(67));
            make.left.offset(kwt(12));
            make.right.offset(kwt(-12));
            make.bottom.offset(kwt(-10));

        }];
        
        UILabel * lab = [DTKTool labelWithText:self.model.tissueListStr andFont:kCFont(15) andTextColor:KcolorHex(@"#626262")];
        [backview addSubview:lab];
        lab.numberOfLines = 0;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kwt(10));
            make.left.offset(kwt(8));
            make.width.offset(kwt(315));
        }];
    }
    
}


- (void)seledBtnClick {
    if(self.seledModelBlock){
        self.seledModelBlock(self.model,!self.model.isSeled);
    }
}
- (void)openBtnClick {
    if(self.openBlock){
        self.model.isOpen = !self.model.isOpen;
        self.openBlock(self.model);
    }
}


@end
